#!/bin/bash

# 检测系统类型
if [ -f "/etc/os-release" ]; then
    os_release_file="/etc/os-release"
    os_name=$(grep -E "^NAME=" "$os_release_file" | awk -F'=' '{print $2}' | tr -d '"')
    os_version=$(grep -E "^VERSION_ID=" "$os_release_file" | awk -F'=' '{print $2}' | tr -d '"')
elif [ -f "/System/Library/CoreServices/SystemVersion.plist" ]; then
    os_release_file="/System/Library/CoreServices/SystemVersion.plist"
    os_name="macOS"
    os_version=$(defaults read "$os_release_file" ProductVersion)
else
    os_name="Windows"
    os_version=""
fi

# 根据系统类型进行处理
if [ "$os_name" == "macOS" ]; then
    echo "此脚本是用于在Ubuntu 22.04系统上切换窗口系统的，不适用于macOS系统，脚本将中断执行。"
    exit 1
elif [ "$os_name" == "Windows" ]; then
    echo "此脚本是用于在Ubuntu 22.04系统上切换窗口系统的，无法在Windows系统上运行，脚本将中断执行。"
    exit 1
elif [ "$os_name" == "Ubuntu" ] && [[ "$os_version" =~ ^22\.04(\.[0-9]+)?$ ]]; then
    # 检查当前使用的窗口系统
    if [ -f "/etc/gdm3/custom.conf" ]; then
        current_window_system=$(grep -E "^WaylandEnable" "/etc/gdm3/custom.conf" | awk -F'=' '{print $2}')
    else
        echo "未找到 /etc/gdm3/custom.conf 文件，无法确定当前窗口系统。"
        exit 1
    fi

    # 定义切换函数
    switch_window_system() {
        if [ "$1" == "x11" ]; then
            if [ "$current_window_system" == " false" ]; then
                echo "X11已经启用。"
                return
            fi
            sudo cp /etc/gdm3/custom.conf /etc/gdm3/custom.conf.bak # 备份配置文件
            sudo sed -i 's/#WaylandEnable=false/WaylandEnable=false/' "/etc/gdm3/custom.conf"
            echo "正在切换到X11窗口系统。系统将重启。"
            sudo reboot
        elif [ "$1" == "wayland" ]; then
            if [ "$current_window_system" != " false" ]; then
                echo "Wayland已经启用。"
                return
            fi
            sudo cp /etc/gdm3/custom.conf /etc/gdm3/custom.conf.bak # 备份配置文件
            sudo sed -i 's/WaylandEnable=false/#WaylandEnable=false/' "/etc/gdm3/custom.conf"
            echo "正在切换到Wayland窗口系统。系统将重启。"
            sudo reboot
        else
            echo "无效的窗口系统选项。请选择'x11'或'wayland'。"
        fi
    }

    # 主程序逻辑
    if [ -z "$current_window_system" ]; then
        echo "无法确定当前窗口系统。请检查 /etc/gdm3/custom.conf。"
        exit 1
    fi

    if [ "$current_window_system" == " false" ]; then
        current_system_name="X11"
    else
        current_system_name="Wayland"
    fi

    echo "当前窗口系统：$current_system_name"
    echo "选择一个选项来切换窗口系统："
    echo "1. 切换到X11"
    echo "2. 切换到Wayland"

    # 读取用户输入并处理
    while true; do
        read -p "请输入选项 (1 或 2): " option
        option=$(echo "$option" | tr -d '[:space:]')  # 去掉输入的空格

        case $option in
            1)
                if [ "$current_system_name" == "X11" ]; then
                    echo "X11已经是当前窗口系统，无需切换。"
                    exit 0
                fi
                switch_window_system "x11"
                break
                ;;
            2)
                if [ "$current_system_name" == "Wayland" ]; then
                    echo "Wayland已经是当前窗口系统，无需切换。"
                    exit 0
                fi
                switch_window_system "wayland"
                break
                ;;
            *)
                echo "无效选项。请选择1或2。别发泡"
                ;;
        esac
    done
else
    echo "此脚本仅能在Ubuntu 22.04或22.04.x 系统上运行，当前系统为：$os_name $os_version，无法继续执行操作。"
    exit 1
fi
